<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CREATE RULE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CREATE ROLE"
HREF="sql-createrole.html"><LINK
REL="NEXT"
TITLE="CREATE SCHEMA"
HREF="sql-createschema.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="CREATE ROLE"
HREF="sql-createrole.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="CREATE SCHEMA"
HREF="sql-createschema.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CREATERULE"
></A
>CREATE RULE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN66815"
></A
><H2
>Name</H2
>CREATE RULE&nbsp;--&nbsp;define a new rewrite rule</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN66820"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE [ OR REPLACE ] RULE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> AS ON <TT
CLASS="REPLACEABLE"
><I
>event</I
></TT
>
    TO <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
> [ WHERE <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> ]
    DO [ ALSO | INSTEAD ] { NOTHING | <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> | ( <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> ; <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> ... ) }</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66829"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE RULE</TT
> defines a new rule applying to a specified
   table or view.
   <TT
CLASS="COMMAND"
>CREATE OR REPLACE RULE</TT
> will either create a
   new rule, or replace an existing rule of the same name for the same
   table.
  </P
><P
>   The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> rule system allows one to
   define an alternative action to be performed on insertions, updates,
   or deletions in database tables.  Roughly speaking, a rule causes
   additional commands to be executed when a given command on a given
   table is executed.  Alternatively, an <TT
CLASS="LITERAL"
>INSTEAD</TT
>
   rule can replace a given command by another, or cause a command
   not to be executed at all.  Rules are used to implement table
   views as well.  It is important to realize that a rule is really
   a command transformation mechanism, or command macro.  The
   transformation happens before the execution of the commands starts.
   If you actually want an operation that fires independently for each
   physical row, you probably want to use a trigger, not a rule.
   More information about the rules system is in <A
HREF="rules.html"
>Chapter 37</A
>.
  </P
><P
>   Presently, <TT
CLASS="LITERAL"
>ON SELECT</TT
> rules must be unconditional
   <TT
CLASS="LITERAL"
>INSTEAD</TT
> rules and must have actions that consist
   of a single <TT
CLASS="COMMAND"
>SELECT</TT
> command.  Thus, an
   <TT
CLASS="LITERAL"
>ON SELECT</TT
> rule effectively turns the table into
   a view, whose visible contents are the rows returned by the rule's
   <TT
CLASS="COMMAND"
>SELECT</TT
> command rather than whatever had been
   stored in the table (if anything).  It is considered better style
   to write a <TT
CLASS="COMMAND"
>CREATE VIEW</TT
> command than to create a
   real table and define an <TT
CLASS="LITERAL"
>ON SELECT</TT
> rule for it.
  </P
><P
>   You can create the illusion of an updatable view by defining
   <TT
CLASS="LITERAL"
>ON INSERT</TT
>, <TT
CLASS="LITERAL"
>ON UPDATE</TT
>, and
   <TT
CLASS="LITERAL"
>ON DELETE</TT
> rules (or any subset of those that's
   sufficient for your purposes) to replace update actions on the view
   with appropriate updates on other tables.  If you want to support
   <TT
CLASS="COMMAND"
>INSERT RETURNING</TT
> and so on, then be sure to put a suitable
   <TT
CLASS="LITERAL"
>RETURNING</TT
> clause into each of these rules.  Alternatively,
   an updatable view can be implemented using <TT
CLASS="LITERAL"
>INSTEAD OF</TT
>
   triggers (see <A
HREF="sql-createtrigger.html"
>CREATE TRIGGER</A
>).
  </P
><P
>   There is a catch if you try to use conditional rules for view
   updates: there <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>must</I
></SPAN
> be an unconditional
   <TT
CLASS="LITERAL"
>INSTEAD</TT
> rule for each action you wish to allow
   on the view.  If the rule is conditional, or is not
   <TT
CLASS="LITERAL"
>INSTEAD</TT
>, then the system will still reject
   attempts to perform the update action, because it thinks it might
   end up trying to perform the action on the dummy table of the view
   in some cases.  If you want to handle all the useful cases in
   conditional rules, add an unconditional <TT
CLASS="LITERAL"
>DO
   INSTEAD NOTHING</TT
> rule to ensure that the system
   understands it will never be called on to update the dummy table.
   Then make the conditional rules non-<TT
CLASS="LITERAL"
>INSTEAD</TT
>; in
   the cases where they are applied, they add to the default
   <TT
CLASS="LITERAL"
>INSTEAD NOTHING</TT
> action.  (This method does not
   currently work to support <TT
CLASS="LITERAL"
>RETURNING</TT
> queries, however.)
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66862"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>      The name of a rule to create.  This must be distinct from the
      name of any other rule for the same table.  Multiple rules on
      the same table and same event type are applied in alphabetical
      name order.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>event</I
></TT
></DT
><DD
><P
>      The event is one of <TT
CLASS="LITERAL"
>SELECT</TT
>,
      <TT
CLASS="LITERAL"
>INSERT</TT
>, <TT
CLASS="LITERAL"
>UPDATE</TT
>, or
      <TT
CLASS="LITERAL"
>DELETE</TT
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
></DT
><DD
><P
>      The name (optionally schema-qualified) of the table or view the
      rule applies to.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
></DT
><DD
><P
>      Any <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> conditional expression (returning
      <TT
CLASS="TYPE"
>boolean</TT
>).  The condition expression cannot refer
      to any tables except <TT
CLASS="LITERAL"
>NEW</TT
> and <TT
CLASS="LITERAL"
>OLD</TT
>, and
      cannot contain aggregate functions.
     </P
></DD
><DT
><TT
CLASS="OPTION"
>INSTEAD</TT
></DT
><DD
><P
><TT
CLASS="LITERAL"
>INSTEAD</TT
> indicates that the commands should be
      executed <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>instead of</I
></SPAN
> the original command.
     </P
></DD
><DT
><TT
CLASS="OPTION"
>ALSO</TT
></DT
><DD
><P
><TT
CLASS="LITERAL"
>ALSO</TT
> indicates that the commands should be
      executed <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>in addition to</I
></SPAN
> the original
      command.
     </P
><P
>      If neither <TT
CLASS="LITERAL"
>ALSO</TT
> nor
      <TT
CLASS="LITERAL"
>INSTEAD</TT
> is specified, <TT
CLASS="LITERAL"
>ALSO</TT
>
      is the default.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
></DT
><DD
><P
>      The command or commands that make up the rule action.  Valid
      commands are <TT
CLASS="COMMAND"
>SELECT</TT
>,
      <TT
CLASS="COMMAND"
>INSERT</TT
>, <TT
CLASS="COMMAND"
>UPDATE</TT
>,
      <TT
CLASS="COMMAND"
>DELETE</TT
>, or <TT
CLASS="COMMAND"
>NOTIFY</TT
>.
     </P
></DD
></DL
></DIV
><P
>   Within <TT
CLASS="REPLACEABLE"
><I
>condition</I
></TT
> and
   <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>, the special
   table names <TT
CLASS="LITERAL"
>NEW</TT
> and <TT
CLASS="LITERAL"
>OLD</TT
> can
   be used to refer to values in the referenced table.
   <TT
CLASS="LITERAL"
>NEW</TT
> is valid in <TT
CLASS="LITERAL"
>ON INSERT</TT
> and
   <TT
CLASS="LITERAL"
>ON UPDATE</TT
> rules to refer to the new row being
   inserted or updated.  <TT
CLASS="LITERAL"
>OLD</TT
> is valid in
   <TT
CLASS="LITERAL"
>ON UPDATE</TT
> and <TT
CLASS="LITERAL"
>ON DELETE</TT
> rules
   to refer to the existing row being updated or deleted.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66932"
></A
><H2
>Notes</H2
><P
>   You must be the owner of a table to create or change rules for it.
  </P
><P
>   In a rule for <TT
CLASS="LITERAL"
>INSERT</TT
>, <TT
CLASS="LITERAL"
>UPDATE</TT
>, or
   <TT
CLASS="LITERAL"
>DELETE</TT
> on a view, you can add a <TT
CLASS="LITERAL"
>RETURNING</TT
>
   clause that emits the view's columns.  This clause will be used to compute
   the outputs if the rule is triggered by an <TT
CLASS="COMMAND"
>INSERT RETURNING</TT
>,
   <TT
CLASS="COMMAND"
>UPDATE RETURNING</TT
>, or <TT
CLASS="COMMAND"
>DELETE RETURNING</TT
> command
   respectively.  When the rule is triggered by a command without
   <TT
CLASS="LITERAL"
>RETURNING</TT
>, the rule's <TT
CLASS="LITERAL"
>RETURNING</TT
> clause will be
   ignored.  The current implementation allows only unconditional
   <TT
CLASS="LITERAL"
>INSTEAD</TT
> rules to contain <TT
CLASS="LITERAL"
>RETURNING</TT
>; furthermore
   there can be at most one <TT
CLASS="LITERAL"
>RETURNING</TT
> clause among all the rules
   for the same event.  (This ensures that there is only one candidate
   <TT
CLASS="LITERAL"
>RETURNING</TT
> clause to be used to compute the results.)
   <TT
CLASS="LITERAL"
>RETURNING</TT
> queries on the view will be rejected if
   there is no <TT
CLASS="LITERAL"
>RETURNING</TT
> clause in any available rule.
  </P
><P
>   It is very important to take care to avoid circular rules.  For
   example, though each of the following two rule definitions are
   accepted by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, the
   <TT
CLASS="COMMAND"
>SELECT</TT
> command would cause
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> to report an error because
   of recursive expansion of a rule:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE RULE "_RETURN" AS
    ON SELECT TO t1
    DO INSTEAD
        SELECT * FROM t2;

CREATE RULE "_RETURN" AS
    ON SELECT TO t2
    DO INSTEAD
        SELECT * FROM t1;

SELECT * FROM t1;</PRE
><P>
  </P
><P
>   Presently, if a rule action contains a <TT
CLASS="COMMAND"
>NOTIFY</TT
>
   command, the <TT
CLASS="COMMAND"
>NOTIFY</TT
> command will be executed
   unconditionally, that is, the <TT
CLASS="COMMAND"
>NOTIFY</TT
> will be
   issued even if there are not any rows that the rule should apply
   to.  For example, in:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE RULE notify_me AS ON UPDATE TO mytable DO ALSO NOTIFY mytable;

UPDATE mytable SET name = 'foo' WHERE id = 42;</PRE
><P>
   one <TT
CLASS="COMMAND"
>NOTIFY</TT
> event will be sent during the
   <TT
CLASS="COMMAND"
>UPDATE</TT
>, whether or not there are any rows that
   match the condition <TT
CLASS="LITERAL"
>id = 42</TT
>.  This is an
   implementation restriction that might be fixed in future releases.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN66964"
></A
><H2
>Compatibility</H2
><P
>   <TT
CLASS="COMMAND"
>CREATE RULE</TT
> is a
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> language extension, as is the
   entire query rewrite system.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createrole.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createschema.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CREATE ROLE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>CREATE SCHEMA</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>